package com.xbongbong.crm.help;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.pro.domain.entity.FormConfigEntity;
import com.xbongbong.saas.enums.FormConfigEnum;
import com.xbongbong.saas.enums.RepeatTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.model.FormConfigModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/01/19 15:11
 */
@Component
public class PhoneHelp {

    @Resource
    private FormConfigModel formConfigModel;

    /**
     * 查询客户或线索名称和电话判重设置
     *
     * @param corpid
     * @param formId
     * @param explainList 解释字段
     * @param businessType 业务类型
     * @author xingxing.xiao
     * @date 2019/11/12 20:08
     */
    public void handlerCustomerRepeat(String corpid, Long formId, List<FieldAttrEntity> explainList, Integer businessType) {
        String nameRepeat = FormConfigEnum.CUSTOMER_NAME_REPEAT_TYPE.getConfig();
        String phoneRepeat = FormConfigEnum.CUSTOMER_PHONE_REPEAT_TYPE.getConfig();
        String nameAttr = CustomerManagementEnum.NAME.getAttr();
        String phoneAttr = CustomerManagementEnum.PHONE.getAttr();
        if (Objects.equals(businessType, XbbRefTypeEnum.CLUE.getCode())) {
            nameRepeat = FormConfigEnum.CLUE_NAME_REPEAT_TYPE.getConfig();
            phoneRepeat = FormConfigEnum.CLUE_PHONE_REPEAT_TYPE.getConfig();
            nameAttr = ClueEnum.COMPANY_NAME.getAttr();
            phoneAttr = ClueEnum.PHONE.getAttr();
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("formId", formId);
        param.put("configIn", Arrays.asList(nameRepeat, phoneRepeat));
        param.put("del", DelEnum.NORMAL.getDel());
        List<FormConfigEntity> formConfigEntityList = formConfigModel.findEntitys(param);
        Map<String, String> formConfigMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for ( FormConfigEntity configEntity : formConfigEntityList ) {
            formConfigMap.put(configEntity.getConfig(), configEntity.getConfigValue());
        }

        if ( Objects.nonNull(formConfigMap.get(nameRepeat)) ) {
            for ( FieldAttrEntity fieldAttrEntity : explainList ) {
                if ( Objects.equals(fieldAttrEntity.getAttr(), nameAttr) ) {
                    // 根据表单配置更改 noRepeat 的值(不启用时noRepeat为0，模板内和全局时noRepeat为1)
                    if ( Objects.equals(formConfigMap.get(nameRepeat), RepeatTypeEnum.REPEAT_CANCEL.getType().toString()) ) {
                        fieldAttrEntity.setNoRepeat(RepeatTypeEnum.REPEAT_CANCEL.getType());
                    } else {
                        fieldAttrEntity.setNoRepeat(RepeatTypeEnum.REPEAT_SINGLE.getType());
                    }
                    break;
                }
            }
        }

        if ( Objects.nonNull(formConfigMap.get(phoneRepeat)) ) {
            for ( FieldAttrEntity fieldAttrEntity : explainList ) {
                if ( Objects.equals(fieldAttrEntity.getAttr(), phoneAttr) ) {
                    List<? extends FieldAttrEntity> items = fieldAttrEntity.getSubForm().getItems();
                    for ( FieldAttrEntity item : items ) {
                        if ( Objects.equals(item.getAttr(), "text_2") ) {
                            // 根据表单配置更改 noRepeat 的值(不启用时noRepeat为0，模板内和全局时noRepeat为1)
                            if ( Objects.equals(formConfigMap.get(phoneRepeat), RepeatTypeEnum.REPEAT_CANCEL.getType().toString()) ) {
                                item.setNoRepeat(RepeatTypeEnum.REPEAT_CANCEL.getType());
                            } else {
                                item.setNoRepeat(RepeatTypeEnum.REPEAT_SINGLE.getType());
                            }
                        }
                    }
                    break;
                }
            }
        }
    }
}
